import axios from 'axios'
import api from "./server-api";

var _url = api.ip
// var _url = 'http://47.107.159.133:80'

const typesList = (callback) => {
  const url = _url + '/types/'
  axios.get(url).then(num => {
    callback && callback(num.data)
  })
}

//分页
const pagingList = (val,input,callback) => {
  var inputUrl;
  if (input == "" || input == null){
    inputUrl =  '/types/?limit=5&pages='+val
  }else {
    inputUrl = '/types/?limit=5&pages='+val+'&name=' + input
  }
  const url = _url  + inputUrl
  axios.get(url).then(num => {
    callback && callback(num.data)
  })
}

//添加小类内容中的大类选择
const categoryList = (callback) => {
  const url = _url + '/category/list'
  axios.get(url).then(num => {
    callback && callback(num.data)
  })
}

const dialogFormVisiblePostApi = (listPost, callback) => {
  var _listPut = new URLSearchParams();
  //添加自动追加id
  _listPut.append("typeName",listPost.name);
  _listPut.append("categoryId",listPost.id);



  const url = _url + '/types/?' + _listPut;
  axios.post(url).then(num => {
    callback && callback(num.data);
  })
}

//搜索
const searchApi = (input, callback) => {
  const url = _url + '/types/?name=' + input;
  axios.get(url).then(num => {
    callback && callback(num.data);
  })
}


const typesDelApi = (typesId, callback) => {
  const url = _url + '/types/?typeId=' + typesId
  axios.delete(url).then(num => {
    console.log(num);
    callback && callback(num)
  })
}


export {
  typesList, // 自动刷新 只查5条数据
  pagingList, //分页
  categoryList,//添加小类内容中的大类选择
  dialogFormVisiblePostApi,//添加小类
  searchApi,//搜索
  typesDelApi,//删除
}
